/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yufei.toolinterface.kwInterface;

import java.util.Collections;
import java.util.List;

import javax.swing.JDialog;

import com.yufei.infoExtractor.core.InfoExtractorMI;
import com.yufei.infoExtractor.core.impl.InfoExtractorM;
import com.yufei.infoExtractor.pfw.entity.News;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.toolinterface.util.DialogUtil;

/**
 *
 * @author jasstion
 */
public class KeyWSearchDemo extends javax.swing.JFrame {
	private InfoExtractorMI infoExtractorMI=(InfoExtractorMI) AppUtil.getBeanFromBeanContainer(InfoExtractorM.class);

    /**
     * Creates new form KeyWSearchDemo
     */
    public KeyWSearchDemo() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        keyWordsLable = new javax.swing.JLabel();
        keyWords = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        statusInfo = new javax.swing.JTextArea();
        statusInfoLabel = new javax.swing.JLabel();
        listButton = new javax.swing.JButton();
        exportButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("关键字查询Demo");

        keyWordsLable.setText("关键字");

        keyWords.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyWordsActionPerformed(evt);
            }
        });

        searchButton.setText("启动查询");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        statusInfo.setEditable(false);
        statusInfo.setColumns(20);
        statusInfo.setRows(5);
        jScrollPane1.setViewportView(statusInfo);

        statusInfoLabel.setText("系统运行状态信息");

        listButton.setText("信息采集列表");
        listButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listButtonActionPerformed(evt);
            }
        });

        exportButton.setText("导出采集结果");
        exportButton.setEnabled(false);

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("以上功能仅仅限于Demo，暂不支持系统运行状态显示，采集数据结果的多样化导出以及各种系统运行参数\n设置等，如需要请联系本人进行定制。\n联系方式：\n邮箱：zhao-0244@qq.com\n手机：15210834071\n");
        jScrollPane2.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(230, 230, 230))
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(keyWordsLable)
                        .addGap(18, 18, 18)
                        .addComponent(keyWords, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(searchButton))
                    .addComponent(statusInfoLabel))
                .addContainerGap(182, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(listButton)
                .addGap(58, 58, 58)
                .addComponent(exportButton)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jScrollPane2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(keyWordsLable)
                    .addComponent(keyWords, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusInfoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(listButton)
                    .addComponent(exportButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void keyWordsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyWordsActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_keyWordsActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
    	String tempKeyWords=this.keyWords.getText().trim();
    	if(CommonUtil.isEmptyOrNull(tempKeyWords)){
    		DialogUtil.popUpDialog("请输入关键字！");
    		return;
    	}
    	infoExtractorMI.startTask(tempKeyWords);
		DialogUtil.popUpDialog("查询结束，可以点击按钮‘信息采集列表’查看采集的信息！");

    	
    }//GEN-LAST:event_searchButtonActionPerformed

    private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listButtonActionPerformed
        // TODO add your handling code here:
    	String collectionName0 = "com.yufei.infoExtractor.pfw.entity.News";
    	List<News> resultList =null;
		try {
	    resultList= (List<News>) infoExtractorMI.queryEntity(collectionName0);
	   Collections.sort(resultList);
		Class elementType=Class.forName(collectionName0);
		JDialog listDialog = DialogUtil.createDialogForCollectionInformation(this, false, resultList,elementType);
		listDialog.setLocationRelativeTo(this);
		listDialog.pack();
		listDialog.setResizable(true);
		listDialog.setLocationRelativeTo(this);
		listDialog.setVisible(true);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     
    }//GEN-LAST:event_listButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(KeyWSearchDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KeyWSearchDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KeyWSearchDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KeyWSearchDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                KeyWSearchDemo keyWSearchDemo = new KeyWSearchDemo();
                keyWSearchDemo.setTitle("InfoExtractor信息采集系统");
                //keyWSearchDemo.setIconImage(new Im);
                keyWSearchDemo.setResizable(false);
				keyWSearchDemo.setVisible(true);
				keyWSearchDemo.setLocationRelativeTo(null);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton exportButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField keyWords;
    private javax.swing.JLabel keyWordsLable;
    private javax.swing.JButton listButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextArea statusInfo;
    private javax.swing.JLabel statusInfoLabel;
    // End of variables declaration//GEN-END:variables
}
